package com.cxk.hr.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cxk.hr.dao.DepartmentMapper;
import com.cxk.hr.dao.EmployeeMapper;
import com.cxk.hr.dao.HistoryMapper;
import com.cxk.hr.dao.MoveMapper;
import com.cxk.hr.dao.PositionMapper;
import com.cxk.hr.pojo.Attendance;
import com.cxk.hr.pojo.Department;
import com.cxk.hr.pojo.Employee;
import com.cxk.hr.pojo.Leave;
import com.cxk.hr.pojo.Overtime;
import com.cxk.hr.pojo.Position;
import com.cxk.hr.service.EmployeeService;

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {
	@Resource
	private EmployeeMapper employeeMapper;
	@Resource
	private HistoryMapper historyMapper;
	@Resource
	private DepartmentMapper departmentMapper;
	@Resource
	private PositionMapper positionMapper;
	@Resource
	private MoveMapper moveMapper;

	/**
	 * 为employee对象setDepartment setPosition的方法
	 * 
	 * @param employee
	 * @return
	 */
	public Employee setObject(Employee employee) {
		Integer departmentNumber = employee.getDepartmentNumber();
		Department department = departmentMapper.selectByNumber(departmentNumber);
		employee.setDepartment(department);

		Integer positionNumber = employee.getPositionNumber();
		Position position = positionMapper.selectByNumber(positionNumber);
		employee.setPosition(position);
		return employee;
	}

	@Override
	public Employee checkLogin(Integer employeeNumber, String password) {
		Employee employee = employeeMapper.checkLogin(employeeNumber, password);
		if (employee != null) {
			// 为employee对象中setDepartment setPosition
			setObject(employee);
		}
		return employee;
	}

	@Override
	public List<Employee> selectListByPage(Integer pageNo, Integer pageSize) {
		List<Employee> employees = employeeMapper.selectListByPage(pageNo, pageSize);
		return employees;
	}

	@Override
	public Integer addEmployee(Employee employee) {
		Integer isAdd = employeeMapper.addEmployee(employee);
		return isAdd;

	}

	@Override
	public Employee selectEmployeeById(Integer id) {
		Employee employee = employeeMapper.selectEmployeeById(id);
		return employee;
	}

	@Override
	public Integer updateEmployee(Employee employee, String status, String manager) {
		Integer isUpdate = employeeMapper.updateEmployee(employee, status, manager);
		Integer isUpdate2 = historyMapper.updateHistoryByEmployee(employee, status);
		return isUpdate * 10 + isUpdate2;

	}

	@Override
	public Integer deleteEmployee(Integer id) {
		Integer isDel = employeeMapper.deleteEmployee(id);
		return isDel;
	}

	@Override
	public Employee selectByNumber(Integer employeeNumber) {
		Employee employee = employeeMapper.selectByNumber(employeeNumber);
		return employee;
	}

	@Override
	public List<Employee> search(String input, Integer pageNo) {
		List<Employee> employees = employeeMapper.search(input, pageNo);
		return employees;
	}

	@Override
	public List<Employee> findAll() {
		List<Employee> employees = employeeMapper.findAll();
		return employees;
	}

	/**
	 * 
	 */
	public Employee findEmpById(Integer id) {
		Employee emp = employeeMapper.findEmpById(id);
		return emp;
	}

	public EmployeeMapper getEmployeemapper() {
		return employeeMapper;
	}

	public void setEmployeemapper(EmployeeMapper employeemapper) {
		this.employeeMapper = employeemapper;
	}

	public List<Department> findAlldept() {
		List<Department> dept = employeeMapper.findAlldept();
		return dept;
	}

	public List<Position> findAllpos() {
		List<Position> pos = employeeMapper.findAllpos();
		return pos;
	}

	public List<Attendance> findAllatt(Integer id) {
		List<Attendance> att = employeeMapper.findAllatt(id);
		return att;
	}

	public List<Leave> findAlllea(Integer id) {// 请假信息
		List<Leave> lea = employeeMapper.findAlllea(id);
		return lea;
	}

	public List<Overtime> findAllovertime() {
		List<Overtime> overtime = employeeMapper.findAllovertime();
		return overtime;
	}

	public List<Employee> fingdAllpeo() {
		List<Employee> emppeo = employeeMapper.fingdAllpeo();
		return emppeo;
	}

	public Integer updataEmpById(Employee employee) {
		Integer num = employeeMapper.updataEmpById(employee);
		return num;
	}

	public Department findDepartByName(String name) { // 部门名称查部门编号

		Department dept = employeeMapper.findDepartByName(name);
		return dept;
	}

	public Position findPosByName(String name) {
		Position pos = employeeMapper.findPosByName(name);
		return pos;
	}

	public List<Employee> findEmpByDeptNum(Integer id) {// 通过id查询
		List<Employee> emp = employeeMapper.findEmpByDeptNum(id);
		return emp;
	}

	public Integer addOverTime(Integer departmentNumber, Integer employeeNumber, Date date) {// 新增加班
		Integer i = employeeMapper.addOverTime(departmentNumber, employeeNumber, date);
		return i;
	}

	public Integer addLeave(Integer employeeNumber, Integer departmentNumber, Date startTime, Date endTime, String days,
			String type, String reason) {
		Integer i = employeeMapper.addLeave(employeeNumber, departmentNumber, startTime, endTime, days, type, reason);
		return i;
	}
}
